<template>
	<view class="container">
		<view class="tip-pay">注:此次发布需缴纳{{ serverFlag ? '服务费' : '诚意金'}} : {{ bond }}元/人,应付<span>{{ bond*num }}</span>元
		</view>
		<view class="choose">
			<text>选择支付方式</text>
			<view class="ipt">
				<view @click="handleSelect(2)">
					<view v-if="requestObj.payType != 2" class="circle"></view>
					<image v-if="requestObj.payType == 2" src="https://images.linglinggong.net/icon1/51.png"
						mode="aspectFill"></image>
					<image class="img" src="https://images.linglinggong.net/icon2/05.png" mode="aspectFill"></image>
					<text>钱包支付</text>
					<text class="my-money">(余额: {{ userInfo.amount/100 }} )</text>
				</view>
				<!--  #ifdef MP-ALIPAY || MP-TOUTIAO  -->
				<view @click="handleSelect(0)">
					<view v-if="requestObj.payType != 0" class="circle"></view>
					<image v-if="requestObj.payType == 0" src="https://images.linglinggong.net/icon1/51.png"
						mode="aspectFill"></image>
					<image class="img" src="https://images.linglinggong.net/icon1/26.png" mode="aspectFill"></image>
					<text>支付宝支付</text>
				</view>
				<!-- #endif -->
				<!--  #ifdef MP-WEIXIN || MP-TOUTIAO  -->
				<view @click="handleSelect(3)">
					<view v-if="requestObj.payType != 3" class="circle"></view>
					<image v-if="requestObj.payType == 3" src="https://images.linglinggong.net/icon1/51.png"
						mode="aspectFill"></image>
					<image class="img" src="https://images.linglinggong.net/icon1/53.png" mode="aspectFill"></image>
					<text>微信支付</text>
				</view>
				<!-- #endif -->
			</view>
		</view>
		<view class="main">
			<text v-if="!serverFlag"
				style="color: #E02020; font-size: 18px;font-weight: bold;">诚意金在订单完成后，可抵扣工人工资。</text>
			<text
				style="color: #8f8f8f; margin-top: 10rpx;">{{serverFlag ? '为了提高服务质量，诚意金将转化成平台服务费。' : '为了保障服务质量，请勿进行线下交易，否则会导致诚意金无法退回。'}}</text>
		</view>
		<view class="confirm" @click="handlePayBond">确认发布</view>
		<!-- 支付密码弹框 -->
		<view v-if="isOpenModal" class="share-modal">
			<view class="main" :class="{'errorpw':isShowPassWordAnimation}">
				<image @click="handleCloseModal" class="close" src="https://images.linglinggong.net/icon1/47.png">
				</image>
				<view class="modal-title">
					<text>请输入钱包支付密码</text>
				</view>
				<view class="pay-code">
					<input type="number" :focus="focus" v-model="requestObj.payPwd" maxlength="6" @input="getPayCode" />
					<block v-for="(item, index) in ranges" :key="index">
						<view :class="['box', { active: codeIndex === item}]">
							<view class="line"></view>
							<text>{{ codeArr[index] ? codeArr[index] : ''}}</text>
							<!-- <text v-if="!isShowCode" style="transform: translateY(10%);">{{ codeArr[index] ? '*' : ''}}</text> -->
						</view>
					</block>
				</view>
				<view class="repwd" v-if="isShowForgetPassword" @click="toRePasssWord">忘记密码</view>
				<!-- <view class="button" @click="handleComfirmPay">确认</view> -->
			</view>
		</view>
	</view>
</template>
<script>
	import Voice from '@/libs/QS-baiduyy.js'
	export default {
		data() {
			return {
				service_task_num: 20, //免费发单数量
				serverFlag: 0, //0:付诚意金，1付服务费
				//isShowCode: true,
				focus: false, //键盘输入焦点
				codeIndex: 1,
				codeArr: [], //密码
				ranges: [1, 2, 3, 4, 5, 6],
				isOpenModal: false, //是否打开分享弹框
				userInfo: {
					user: {},
					params: {}
				}, //当前用户信息
				requestObj: {
					payPwd: '',
					// #ifdef MP-ALIPAY || MP-TOUTIAO
					payType: 0, //0支付宝，1微信，2钱包  //taskId cardId
					// #endif
					// #ifdef MP-WEIXIN
					payType: 3, //0支付宝，1微信，2钱包  //taskId cardId
					//#endif
					taskId: '',
					paybegtype: ''
				},
				bond: '', //每人诚意金
				num: '', //人数
				isShowForgetPassword: false, //是否弹出忘记密码
				isShowPassWordAnimation: false, //密码错误执行动画
			};
		},
		methods: {
			// 得到输入的密码
			getPayCode(e) {
				let {
					value
				} = e.detail
				let del = false
				this.requestObj.payPwd = value
				if (value.length < this.codeArr.length) {
					del = true
				}
				this.codeArr.length = value.length
				this.codeArr.fill("●")
				this.codeIndex = this.codeArr.length + 1
				if (del) {
					return
				}
				this.codeArr[value.length - 1] = value[value.length - 1]
				setTimeout(() => {
					if (this.codeArr.length != 0) {
						this.$set(this.codeArr, [this.codeArr.length - 1], "●")
					}
				}, 1000)
				if (this.codeIndex == 7) {
					this.handleComfirmPay()
				}
				console.log('codeArr', this.codeIndex)
			},
			//点击链接至账户安全。
			toRePasssWord() {
				uni.navigateTo({
					url: '/pages/account-security/account-security'
				})
			},
			// 钱包支付确认
			async handleComfirmPay() {
				console.log('钱包支付确认', this.requestObj)
				let res = await this.$fetch(this.$api.v1_pay_bond, this.requestObj, 'POST')
				console.log('钱包支付确认', res)
				if (res.code) {
					this.emptyInput()
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					if (res.msg == '结算任务失败请设置支付密码') {
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/account-security/account-security'
							})
						}, 1200)
					}
					return
				} else {
					this.isOpenModal = false
					this.handleCloseModal()
					Voice("您的任务已发布，请耐心等待审核")
					uni.showToast({
						title: '发布成功~',
						icon: 'none'
					})
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/my-job/boss-my-job-new'
						})
					}, 800)
				}
			},
			//清空输入框，更新是否弹出忘记密码标识
			emptyInput() {
				this.isShowPassWordAnimation = true
				this.isShowForgetPassword = true
				this.requestObj.payPwd = ''
				this.codeArr = []
				this.codeIndex = 1
				setTimeout(() => {
					this.isShowPassWordAnimation = false
				}, 1200)
			},
			// 关闭分享弹框
			handleCloseModal() {
				this.isOpenModal = false
				this.requestObj.payPwd = ''
				this.codeArr = []
				this.codeIndex = 1
			},
			//选择提现类型
			handleSelect(index) {
				this.requestObj.payType = index
				if (index == 2) {
					if (uni.getStorageSync('isHavePayPassword') == false) {
						this.setPayPassword()
					}
				}
			},
			// 确认发布，
			async handlePayBond() {
				//支付宝支付
				if (this.requestObj.payType == 0) {
					let res = await this.$fetch(this.$api.v1_pay_bond, this.requestObj, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					uni.requestPayment({
						provider: 'alipay',
						orderInfo: res.msg, //微信、支付宝订单数据
						success: function(res) {
							console.log('success:' + JSON.stringify(res));
							uni.showToast({
								title: '发布成功~',
								icon: 'none'
							})
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/my-job/boss-my-job-new'
								})
							}, 800)
						},
						fail: function(err) {
							console.log('fail:' + JSON.stringify(err));
						}
					})
				} else if (this.requestObj.payType == 2) {
					if (uni.getStorageSync('isHavePayPassword') == false) {
						this.setPayPassword()
					} else {
						this.isOpenModal = true
						this.focus = true
					}
				} else {
					//微信支付
					this.requestObj.paybegtype = 'wx'
					let res = await this.$fetch(this.$api.v1_pay_bond, this.requestObj, 'POST')
					if (res.code) return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					const info = res.data
					console.log(res.data)
					// #ifdef MP-WEIXIN
					this.MP_WXPay(info)
					// #endif
				}
			},
			/* 微信小程序支付 */
			MP_WXPay(info) {
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: info.timeStamp, //时间戳
					nonceStr: info.nonceStr,
					package: info.package,
					signType: info.signType,
					paySign: info.paySign,
					success: res => {
						console.log('success:' + JSON.stringify(res));
						uni.showToast({
							title: '发布成功~',
							icon: 'none'
						})
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/my-job/boss-my-job-new'
							})
						}, 800)
					},
					fail: err => {
						console.log('fail:' + JSON.stringify(err));
					}
				})
			},
			// 获取数据配置
			async getBond() {
				let res4 = await this.$fetch(this.$api.property_list_by_type, {
					type: 1
				}, 'POST')
				//console.log(res4.data.bondS)
				let ls = res4.data.bondS.split(",");
				if (uni.getStorageSync("level") == "1") {
					uni.setStorageSync('bond', ls[0])
					this.bond = ls[0]
				} else {
					uni.setStorageSync('bond', ls[1])
					this.bond = ls[1]
				}
				//console.log(ls[0])
				// 发布任务时 缴纳保证金配置(元/人)
				uni.setStorageSync('service_task_num', res4.data.service_task_num) //雇主免费发单数量，超出缴纳平台服务费
				//this.bond = uni.getStorageSync('bond')
				this.service_task_num = uni.getStorageSync('service_task_num') //免费发单任务数
				console.log('this.service_task_num', this.service_task_num)
			},
			// 获取任务详情
			async task_detail(taskId) {
				let res = await this.$fetch(this.$api.task_detail, {
					taskId: taskId
				}, 'POST')
				// console.log('获取任务详情', res.data)
				this.num = res.data.number //任务人数
			},
			// 获取当前用户信息，及服务标识
			async getUserInfo() {
				let res = await this.$fetch(this.$api.get_current_user, {}, 'GET')
				//	console.log(res)
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				//console.log(res.data)
				uni.setStorageSync('userInfo', res.data)
				uni.setStorageSync('userType', res.data.userType)
				uni.setStorageSync("level", res.data.user.level) //用户等级
				this.userInfo = res.data
				this.serverFlag = this.userInfo.user.serverFlag
				if (this.userInfo.amount != 0) {
					this.requestObj.payType = 2
				}
			},
			setPayPassword() {
				uni.showModal({
					title: '您还未设置支付密码',
					content: '是否现在就去设置支付密码？',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/set-pay-code/set-pay-code'
							})
						} else if (res.cancel) {
							uni.showToast({
								title: '未设置支付密码~',
								icon: 'none'
							})
						}
					}
				})
			}
		},
		onLoad(options) {
			this.requestObj.taskId = options.taskId
			this.task_detail(options.taskId)
		},
		onShow() {
			this.getBond()
			this.getUserInfo()
			// if(uni.getStorageSync('isHavePayPassword') == true) this.requestObj.payType = 2
		},
	}
</script>
<style lang="less" scoped>
	.container {
		overflow: hidden;

		&>.share-modal {
			z-index: 11;
			position: fixed;
			bottom: 0;
			height: 100vh;
			width: 100vw;
			background-color: rgba(0, 0, 0, 0.40);
			display: flex;
			align-items: center;
			justify-content: center;

			&>.main {
				// border: 2rpx solid #000000;
				position: relative;
				top: -100rpx;
				width: 570rpx;
				height: 250rpx;
				padding: 30rpx;
				background: #FFFFFF;
				box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.05) inset;
				border-radius: 17px;
				line-height: 40rpx;
				display: flex;
				flex-direction: column;
				align-items: center;

				&>.modal-title {
					font-size: 17px;
					margin: 30rpx 0 50rpx;

					&>.imgs {
						position: relative;

						image {
							position: absolute;
							top: 0rpx;
							bottom: 0;
							right: -80rpx;
							width: 50rpx;
							height: 40rpx;
						}
					}
				}

				&>.pay-code {
					// border: 2rpx solid #000000;
					padding-bottom: 10rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					text-align: center;
					flex-wrap: wrap;
					position: relative;

					&>input {
						position: absolute;
						background: transparent;
						top: 0;
						left: -100%;
						width: 200%;
						height: 100%;
						text-align: left;
						z-index: 9;
						opacity: 1;
						// border: 1px solid #000000;
					}

					&>.box {
						display: flex;
						align-items: center;
						justify-content: center;
						position: relative;
						width: 80rpx;
						height: 80rpx;
						margin-right: 10rpx;
						font-size: 22px;
						line-height: 90rpx;
						box-sizing: border-box;
						background-color: #F2F2F2;
						border-radius: 2px;

						&:last-child {
							margin-right: 0;
						}
					}

					&>.active {
						border: 2rpx solid #cccccc;

						@keyframes twinkling {
							0% {
								opacity: 0.1;
							}

							50% {
								opacity: 0.8;
							}

							100% {
								opacity: 0.1;
							}
						}

						&>.line {
							display: block;
							position: absolute;
							left: 50%;
							top: 50%;
							transform: translate(-50%, -50%);
							width: 2upx;
							height: 40upx;
							background: #333333;
							animation: twinkling 1s infinite ease;
						}
					}
				}

				.repwd {
					height: 40rpx;
					margin-bottom: -40rpx;
					font-size: 24rpx;
					color: #FE4B28;
				}

				&>.button {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 180rpx;
					height: 70rpx;
					margin-top: 60rpx;
					border-radius: 50rpx;
					background-color: #FCC928;

					&:active {
						opacity: 0.7;
					}
				}

				&>.close {
					position: absolute;
					right: 30rpx;
					top: 30rpx;
					width: 42rpx;
					height: 42rpx;
				}
			}
		}

		&>.tip-pay {
			color: #E02020;
			line-height: 40rpx;
			margin: 30rpx 30rpx 20rpx;
			letter-spacing: 1.5px;

			&>span {
				font-size: 16px;
				font-weight: bold;
			}
		}

		&>.choose {
			background-color: #FFFFFF;
			padding: 30rpx 30rpx 0;

			&>text {
				margin-bottom: 20rpx;
				height: 40rpx;
				position: relative;
				text-align: center;
				display: flex;
				align-items: center;

				&::before {
					content: "";
					position: absolute;
					left: -30rpx;
					width: 4rpx;
					height: 28rpx;
					background: #FCC928;
					border-radius: 1px;
				}
			}

			&>.ipt {
				&>view {
					position: relative;
					display: flex;
					align-items: center;
					padding: 29rpx 0;

					&>.my-money {
						margin-left: 20rpx;
						font-size: 12px;
						color: rgba(64, 33, 16, 0.50);
					}

					// 横线
					&::after {
						content: "";
						position: absolute;
						bottom: 0;
						right: -30rpx;
						width: 654rpx;
						height: 2rpx;
						background: #F2F2F2;
						border-radius: 1px;
					}

					//圆圈
					.circle {
						width: 33rpx;
						height: 33rpx;
						margin-right: 30rpx;
						border-radius: 50%;
						border: 1px solid rgba(64, 33, 16, 0.30);
					}

					image {
						width: 40rpx;
						height: 40rpx;
						margin-right: 30rpx;
					}

					.img {
						width: 42rpx;
						height: 42rpx;
						margin-right: 20rpx;
					}
				}
			}
		}

		&>.main {
			background-color: #FFFFFF;
			display: flex;
			flex-direction: column;
			padding: 30upx;
			margin-top: 30rpx;
		}

		&>.confirm {
			position: fixed;
			bottom: 0;
			background: #FCC928;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100vw;
			height: 100rpx;

			&:active {
				opacity: 0.7;
			}
		}
	}

	.errorpw {
		animation: swing 1s .15s linear 1;
	}

	@keyframes swing {

		10%,
		90% {
			transform: translate(-4rpx, 0);
		}

		20%,
		80% {
			transform: translate(+4rpx, 0);
		}

		30%,
		70% {
			transform: translate(-6rpx, 0);
		}

		40%,
		60% {
			transform: translate(+6rpx, 0);
		}

		50% {
			transform: translate(-8rpx, 0);
		}
	}
</style>